package com.lineage.server.utils;

public class Random {
  private static final int _max = 32767;
  
  private static int _idx = 0;
  
  private static double[] _value = new double[32768];
  
  private static int _nIndex;
  
  private static double[] _nArray;
  
  static {
    _idx = 0;
    while (_idx < 32768) {
      _value[_idx] = (Math.random() + Math.random() + Math.random() + Math.random() + Math.random()) % 1.0D;
      _idx++;
    } 
    _nIndex = 0;
    _nArray = new double[32768];
    do {
      _nArray[_nIndex] = Math.random();
    } while (getIndex() != 0);
  }
  
  public static int nextInt(int n) {
    _idx &= 0x7FFF;
    return (int)(_value[_idx++] * n);
  }
  
  public static int nextInt(int n, int offset) {
    _idx &= 0x7FFF;
    return offset + (int)(_value[_idx++] * n);
  }
  
  public static boolean nextBoolean() {
    return (nextInt(2) == 1);
  }
  
  public static byte nextByte() {
    return (byte)nextInt(256);
  }
  
  public static long nextLong() {
    long l = (nextInt(2147483647) << 32 + nextInt(2147483647));
    return l;
  }
  
  public static int getInt(int rang) {
    return (int)(getValue() * rang);
  }
  
  private static double getValue() {
    return _nArray[getIndex()];
  }
  
  private static int getIndex() {
    return _nIndex = 0x7FFF & ++_nIndex;
  }
}
